package com.yayohos.locationmanager;

import com.yayohos.locationmanager.helper.LogUtils;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.ToastDialog;

/**
 * @author hanchao
 * demo主页
 * @version 1.0
 */
public class MainAbility extends Ability implements Component.ClickedListener {
    private Text toAbility;
    private Text toAbilitySlice;
    private Text toService;
    private Text toFraction;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        toAbility = (Text) findComponentById(ResourceTable.Id_tv_to_ability);
        toAbilitySlice = (Text) findComponentById(ResourceTable.Id_tv_to_AbilitySlice);
        toService = (Text) findComponentById(ResourceTable.Id_tv_to_Service);
        toFraction = (Text) findComponentById(ResourceTable.Id_tv_to_Fragment);
        toAbility.setClickedListener(this::onClick);
        toFraction.setClickedListener(this::onClick);
        toAbilitySlice.setClickedListener(this::onClick);
        toService.setClickedListener(this::onClick);
        LogUtils.logI(Thread.currentThread().getName());
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_to_ability:
                jumpToPage(SampleAbility.class.getName());
                break;
            case ResourceTable.Id_tv_to_Fragment:
                jumpToPage(SampleFractionAbility.class.getName());
                break;

            case ResourceTable.Id_tv_to_AbilitySlice:
                jumpToPage(SampleSlicePage.class.getName());
                break;
            case ResourceTable.Id_tv_to_Service:
                jumpToPage(SampleServiceAbility.class.getName());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        LogUtils.logE("onBackground=====================");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.logE("onStop=====================");
    }

    private void showTipDialog() {
        ToastDialog toastDialog = new ToastDialog(this);
        toastDialog.setDuration(1000);
        toastDialog.show();
    }

    private void jumpToPage(String pageName) {
        if (TextTool.isNullOrEmpty(pageName)) {
            return;
        }
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(pageName)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }
}
